package com.neu.fragement;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.neu.R;

public class BFragment extends Fragment {

    private BViewModel mViewModel;
    private Button btnToA;
    private AViewModel aViewModel;
    Button btnB;
    TextView tvB;

    public static BFragment newInstance() {
        return new BFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.b_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(BViewModel.class);
        // TODO: Use the ViewModel


        aViewModel=new ViewModelProvider(requireActivity(),new ViewModelProvider.NewInstanceFactory()).get(AViewModel.class);
        aViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                tvB.setText(s);
            }
        });

        btnToA=getActivity().findViewById(R.id.btnToA);

        btnToA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(getView()).navigate(R.id.action_BFragment_to_AFragment);
            }
        });

        btnB=getActivity().findViewById(R.id.btnB);
        btnB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(getView()).navigate(R.id.action_BFragment_to_welcomeActivity);
            }
        });


        //接受來的數據
        Bundle bundle=getArguments();
        String a= (String) bundle.get("a");

        tvB=getActivity().findViewById(R.id.tvB);
        tvB.setText(a);

    }

}